﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MuaChung.Base;
using MuaChung.beatifulhousews;
using MuaChung.doanhnghiepbiz;

namespace MuaChung.Models
{
    public class DonHangDetail
    {
        int donHangID { get; set; }
        int couponID { get; set; }
        int maDonHang { get; set; }
        int maTK { get; set; }
        int maCoupon { get; set; }
        int soLuong { get; set; }
        Single tongGia { get; set; }
        string tenCoupon { get; set; }
        string tenCongTy { get; set; }
        bool daThanhToan { get; set; }
        bool daGiaoHang { get; set; }
        
    }
    public class DonHangDetailMin
    {
        int donHangID { get; set; }
        int couponID { get; set; }
        int maDonHang { get; set; }
        int maTK { get; set; }
        int maCoupon { get; set; }
        int soLuong { get; set; }
        Single tongGia { get; set; }
        string tenCoupon { get; set; }
        string tenCongTy { get; set; }
        bool daThanhToan { get; set; }
        bool daGiaoHang { get; set; }
    }
    public class DonHangModels
    {
        private static MuaChungEntities db = new MuaChungEntities();
        public static List<DONHANG> GetAll(int currentPage,int pageSize)
        {
            try
            {
                int startIndex = pageSize*(currentPage -1);
                var query = (from n in db.DONHANGs
                             orderby n.Ma_DonHang descending
                             select n).Skip(startIndex);
                return query.Take(pageSize).ToList();
            }
            catch
            {
                return null;
            }
        }
        public static List<DONHANG> FindAllByCustomerID(int cid, int currentPage, int pageSize)
        {
            try
            {
                int startIndex = pageSize * (currentPage - 1);
                var query = (from n in db.DONHANGs
                             orderby n.Ma_DonHang descending
                             where n.Ma_TK == cid
                             select n).Skip(startIndex);
                return query.Take(pageSize).ToList();
            }
            catch
            {
                return null;
            }
        }
        public static List<DONHANG> FindAllByCustomerIDAndStatus(int cid, int status, int currentPage, int pageSize)
        {
            try
            {
                int startIndex = pageSize * (currentPage - 1);
                var query = (from n in db.DONHANGs
                             orderby n.Ma_DonHang descending
                             where n.Ma_TK == cid && n.TrangThai == status
                             select n).Skip(startIndex);
                return query.Take(pageSize).ToList();
            }
            catch
            {
                return null;
            }
        }
        public static DONHANG FindByID(int donhangID)
        {
            try
            {
                var query = from n in db.DONHANGs
                            where n.ID == donhangID
                            select n;
                return query.First();
            }
            catch
            {
                return null;
            }
        }
        public static bool Add(DONHANG dh)
        {
            try
            {
                db.AddToDONHANGs(dh);
                db.SaveChanges();
            }
            catch
            {
                return false;
            }
            return true;
        }
        public static bool Delete(DONHANG dh)
        {
            try
            {
                db.DONHANGs.DeleteObject(dh);
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool Update(DONHANG dh)
        {
            try
            {
                db.DONHANGs.ApplyCurrentValues(dh);
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool CancelOrder(int dhID)
        {
            WebServiceDoanhNghiepBiz ws5a = new WebServiceDoanhNghiepBiz();
            Service1 ws2b = new Service1();

            DONHANG dh = FindByID(dhID);
            int wsID = Convert.ToInt32(dh.COUPON.Ma_Webservice);
            WEBSERVICE sv = ServiceModel.FindWSByID(wsID);
            bool code = false;
            try
            {
                if (sv.Ma_WS == BaseWeb.WS_BIZ5A)
                {
                    string sid = ws5a.Authenticate(sv.WSUSERNAME, sv.WSPASSWORD);
                    code = ws5a.CancelOrder(sid, dh.Ma_DonHang);
                }
                else if (sv.Ma_WS == BaseWeb.WS_BIZ2B)
                {
                    string sid = ws2b.Authenticate(sv.WSUSERNAME, sv.WSPASSWORD);
                    code = ws2b.CancelOrder(sid, dh.Ma_DonHang);
                }
                else
                {
                    return false;
                }
                Delete(dh);
            }
            catch
            {
                return false;
            }
            return true;
        }
        public static List<DONHANG> FindAllDonHangByMaCoupon(string cid)
        {
            try
            {
                var query = from n in db.DONHANGs
                            where n.COUPON.Ma_Coupon == cid && n.TrangThai == 1
                            select n;
                return query.ToList();
            }
            catch
            {
                return null;
            }
        }
        public static bool SendEmailToConfirmOrder(int maDH)
        {
            DONHANG dh = DonHangModels.FindByID(maDH);
            string EmailBody = "Xin chào " + AccountModel.FindDetail(dh.TAI_KHOAN.Username).HoTen + ",<br/>";
            EmailBody += "Đơn hàng với mã số "+dh.Ma_DonHang+" mà bạn đã đặt mua tại trang <a href=\""+BaseWeb.WebLink+"\">HotMoiNgay</a> của chúng tôi đã được "+dh.COUPON.DOANHNGHIEP.Ten_DoanhNghiep+" xác nhận.<br/>";
            EmailBody += "Bây giờ bạn đã có thể vào link dưới đây để tiến hành thanh toán và hoàn tất các đơn hàng này.<br/>";
            EmailBody += BaseWeb.WebLink + "Orders/CheckOut <br/>";
            EmailBody += "Thân mến.";

            List<string> recv = new List<string>();
            recv.Add(dh.TAI_KHOAN.Email);

            if (BaseWeb.SendMail("Hot Moi Ngay", BaseWeb.SupportEmailID, BaseWeb.SupportEmailPass, recv, "Hoàn tất đơn hàng", EmailBody))
            {
                return true;
            }
            else
            {
                return false;
            }            
        }
        public static List<DONHANG> FindAllByTime(DateTime dt, int type)
        {
            switch (type)
            {
                case 1:
                    {
                        var query = from n in db.DONHANGs
                                    where n.NgayDatHang.Month == dt.Month && n.NgayDatHang.Year == dt.Year
                                    select n;
                        return query.ToList();
                    }
                case 2:
                    {
                        var query = from n in db.DONHANGs
                                    where n.NgayDatHang.Month >= dt.Month && n.NgayDatHang.Month<dt.Month+3 && n.NgayDatHang.Year == dt.Year
                                    select n;
                        return query.ToList();
                    }
                case 3:
                    {
                        var query = from n in db.DONHANGs
                                    where n.NgayDatHang.Year == dt.Year
                                    select n;
                        return query.ToList();
                    }
            }
            return null;
        }
    }
}